﻿using System.Collections.Generic;
using System.Xml;

namespace DbBuilder
{
	public class ConfigurationFiles : List<ConfigurationFile>
	{
		public ConfigurationFiles (XmlNode node, string key)
		{
			if (node != null)
			{
				foreach (XmlNode fileNode in node.SelectNodes(key))
				{
					Add(new ConfigurationFile(fileNode));
				}
			}
		}
	}

	public class ConfigurationFile
	{
		public readonly string FileName;
		public ConfigurationFile(XmlNode node)
		{
			FileName = node.Attributes["ConfigurationFileName"].Value;
		}
	}

	public class DatabaseEnvironment
	{
		public readonly string Name;
		public readonly string Build;
		public readonly List<Database> Databases = new List<Database>();
		public readonly Security Security;

		public DatabaseEnvironment(string fileName)
		{
			XmlDocument document = new XmlDocument();
			document.Load(fileName);
			Name = document.DocumentElement.Attributes["Name"].Value;
			Build = document.SelectSingleNode("/System/Build").Attributes["Number"].Value;
			foreach (XmlNode node in document.SelectNodes("/System/Databases/Database"))
			{
				Databases.Add(new Database(node));
			}
			Security = new Security(document.SelectSingleNode("/System/Security"));
		}
	}

	public class Database
	{
		public readonly string Name;
		public readonly ConfigurationFiles FileLayout;
		public readonly ConfigurationFiles ObjectDataCreationScript;
		public readonly ConfigurationFiles FullBuildObjectCreation;
		public readonly ConfigurationFiles RolesAndObjectLevelPermissions;
		public readonly ConfigurationFiles StaticData;

		public Database(XmlNode databaseNode)
		{
			Name = databaseNode.Attributes["Name"].Value;
			FileLayout = new ConfigurationFiles(databaseNode, "FileLayout");
			ObjectDataCreationScript = new ConfigurationFiles(databaseNode, "ObjectDataCreationScript");
			FullBuildObjectCreation = new ConfigurationFiles(databaseNode, "FullBuildObjectCreation");
			RolesAndObjectLevelPermissions = new ConfigurationFiles(databaseNode, "RolesAndObjectLevelPermissions");
			StaticData = new ConfigurationFiles(databaseNode, "StaticData");
		}
	}

	public class Security
	{
		public ConfigurationFiles LoginsAndUsers;

		public Security(XmlNode node)
		{
			LoginsAndUsers = new ConfigurationFiles(node, "LoginsAndUsers");
		}
	}
}
